#include "RegisterWindow.h"
#include "ui_RegisterWindow.h"


RegisterWindow::RegisterWindow(QWidget *parent) :BaseWindow(parent),
        ui(new Ui::RegisterWindow)
 {
    ui->setupUi(this);
    QObject::connect(ui->RegisterButton,SIGNAL(clicked()),this,SLOT(OnRegisterClick()));
    QObject::connect(ui->PassEdit,SIGNAL(textEdited(const QString &)),this,SLOT(OnPassChange(const QString &)));

    QObject::connect(ui->LoginEdit,SIGNAL(textEdited(const QString &)),this,SLOT(OnLoginChange(const QString &)));
    QObject::connect(ui->PassEdit,SIGNAL(textEdited(const QString &)),this,SLOT(OnPassChange(const QString &)));
    QObject::connect(ui->EmailEdit,SIGNAL(textEdited(const QString &)),this,SLOT(OnEmailChange(const QString &)));



}
void RegisterWindow::OnLoginChange(const QString & ev)
{
    QPalette pal(ui->LoginEdit->palette());
    pal.setColor(QPalette::Text,Qt::black);
    ui->LoginEdit->setPalette(pal);
}

void RegisterWindow::OnEmailChange(const QString & ev)
{
    QPalette pal(ui->EmailEdit->palette());
    pal.setColor(QPalette::Text,Qt::black);
    ui->EmailEdit->setPalette(pal);
}
void RegisterWindow::OnPassChange(const QString & ev)
{
    QPalette pal(ui->PassEdit->palette());
    pal.setColor(QPalette::Text,Qt::black);
    ui->PassEdit->setPalette(pal);
}
bool RegisterWindow::CheckEmailValid()
{
    if(!IsEmail(ui->EmailEdit->text().toStdString()))
    {

            QPalette pal(ui->EmailEdit->palette());
            pal.setColor(QPalette::Text,Qt::red);
            ui->EmailEdit->setPalette(pal);

            ui->EmailEdit->statusTip();
            return false;
    }
    else
    {
        QPalette pal(ui->EmailEdit->palette());
        pal.setColor(QPalette::Text,Qt::black);
        ui->EmailEdit->setPalette(pal);
    }

    return true;

}
bool RegisterWindow::CheckLoginValid()
{
    if(ui->LoginEdit->text().length()<4)
    {
        if(ui->LoginEdit->text().length()>0)
        {
            QPalette pal(ui->LoginEdit->palette());
            pal.setColor(QPalette::Text,Qt::red);
            ui->LoginEdit->setPalette(pal);

            ui->LoginEdit->statusTip();
            return false;
         }
        else
        {
            QPalette pal(ui->LoginEdit->palette());
            pal.setColor(QPalette::Text,Qt::black);
            ui->LoginEdit->setPalette(pal);
        }
    }

    return true;
}

bool RegisterWindow::CheckPassValid()
{
    if(ui->PassEdit->text().length()<7)
    {
        if(ui->PassEdit->text().length()>0)
        {
            QPalette pal(ui->PassEdit->palette());
            pal.setColor(QPalette::Text,Qt::red);
            ui->PassEdit->setPalette(pal);

            ui->PassEdit->statusTip();
            return false;
         }
        else
        {
            QPalette pal(ui->PassEdit->palette());
            pal.setColor(QPalette::Text,Qt::black);
            ui->PassEdit->setPalette(pal);
        }
    }

    return true;
}

void RegisterWindow::OnRegisterClick()
{
    bool email_valid=CheckEmailValid();
    bool pass_valid=CheckPassValid();
    bool login_valid=CheckLoginValid();
    if(pass_valid && email_valid && login_valid)
    {
        /*ui->RegisterButton->setDisabled(true);
        ui->RegisterButton->setText("Регистрация...");*/

        Satory::GetInstance()->Register(
                ui->LoginEdit->text(),ui->EmailEdit->text(),ui->PassEdit->text(),
                ui->toStorePass->checkState()==Qt::Checked);


     }
}


RegisterWindow::~RegisterWindow()
{
    delete ui;
}


